package com.regex.web.controller.member;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.regex.web.cache.DictCacheService;
import com.regex.web.entity.member.MemberDTO;
import com.regex.web.service.member.IMemberService;
import com.regex.web.utils.AjaxUtil;
import com.regex.web.utils.CipherUtil;
import com.regex.web.utils.QueryParam;
import com.regex.web.utils.QueryResult;

@Controller
@EnableAutoConfiguration
@RequestMapping("sys/member")
public class SysMemberController {
    
	@Value("${RESET_PASSWORD}")
    private  String resetPassword = "111111";
    
    @Autowired
    private DictCacheService memCacheService;

    @Autowired
    private IMemberService memberService;
    
    @RequiresPermissions("member:show")
    @RequestMapping("show")
    public String show(QueryParam param, Model model, String name, String tel) {
        
        QueryWrapper<MemberDTO> queryMember = new QueryWrapper<MemberDTO>();
        if(StringUtils.isNotEmpty(name)) {
            queryMember.like("nick_name", name);
        }
        if(StringUtils.isNotEmpty(tel)) {
            queryMember.like("tel", tel);
        }
        queryMember.orderByDesc("create_time");
        
        Page<MemberDTO> page = new Page<MemberDTO>();
        page.setCurrent(param.getPageNumber());
        page.setSize(param.getPageSize());
        QueryResult<MemberDTO> result = memberService.page(page, queryMember);
        
        
        model.addAttribute("result", result);
        
        Map<String, Object> dictMap = memCacheService.getDictCache();
        model.addAttribute("dictMap", dictMap);
        model.addAttribute("name", name);
        return "sys/member/show";
    }
    
    
    /**
     * 
     * 功能描述: <br>
     * 详情
     *
     * @param id
     * @param model
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequiresPermissions("member:show")
    @RequestMapping("detial")
    public String detial(Long id, Model model) {
        try {
            MemberDTO MemberDTO = memberService.getById(id);
            model.addAttribute("MemberDTO", MemberDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        Map<String, Object> dictMap = memCacheService.getDictCache();
        model.addAttribute("dictMap", dictMap);
        return "sys/member/detial";
    }
    
    /**
     * 
     * 功能描述: <br>
     * 删除
     *
     * @param id
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequiresPermissions("member:del")
    @RequestMapping("del")
    public void del(Long id, HttpServletResponse response) {
        int n = 0;
        boolean falg = memberService.removeById(id);
        if(falg ) {
            n = 1;
        }
        AjaxUtil.ajaxJsonSucMessage(response, n);
    }
    
    /**
     * 
     * 功能描述: <br>
     * 冻结用户
     *
     * @param id
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("frozen")
    public void frozen(Long id, HttpServletResponse response) {
        int n = 0;
        MemberDTO MemberDTO = memberService.getById(id);
        MemberDTO.setState("1");
        boolean flag  = memberService.updateById(MemberDTO);
        if(flag) {
            n = 1;
        }
        AjaxUtil.ajaxJsonSucMessage(response, n);
    }
    
    
    
    @RequiresPermissions("member:reset")
    @RequestMapping("resetPassword")
    public void resetPassword(Long id, HttpServletResponse response) {
        MemberDTO MemberDTO = memberService.getById(id);
        int n = 0;
        if(MemberDTO != null) {
            String password = CipherUtil.encodeByMD5(resetPassword);
            MemberDTO.setPassword(password);
            boolean flag = memberService.updateById(MemberDTO);
            if(flag) {
                n = 1;
            }
        }
        AjaxUtil.ajaxJsonSucMessage(response, n);
    }
    
    /**
     * 
     * 功能描述: <br>
     * 解除冻结
     *
     * @param id
     * @param response
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("relieve")
    public void relieve(Long id, HttpServletResponse response) {
        int n = 0;
        MemberDTO MemberDTO = memberService.getById(id);
        MemberDTO.setState("0");
        boolean flag = memberService.updateById(MemberDTO);
        if(flag) {
            n = 1;
        }
        AjaxUtil.ajaxJsonSucMessage(response, n);
    }
    
    
    
    
    
    /**
     * 
     * 功能描述: <br>
     * 修改/ 新增
     *
     * @param id
     * @param model
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
//    @RequiresPermissions("member:edit")
//    @RequestMapping("input")
//    public String input(Long id, Model model) {
//        if(id!= null && id > 0) {
//            MemberDTO MemberDTO = memberService.selectMemberDTOById(id);
//            model.addAttribute("MemberDTO", MemberDTO);
//        }
//        return "sys/member/input";
//    }
    
    /**
     * 
     * 功能描述: <br>
     * 保存
     *
     * @param MemberDTO
     * @param response
     * @param request
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
//    @RequiresPermissions("member:edit")
//    @RequestMapping("save")
//    public void save(MemberDTO MemberDTO, HttpServletResponse response,
//            HttpServletRequest request) {
//        int n = 0;
//        try {
//        if(StringUtils.isNotEmpty(MemberDTO.getNickName()) && StringUtils.isNotEmpty(MemberDTO.getTel())
//               ) {
//            MemberDTO.setUpdateTime(new Date());
//            if(MemberDTO.getId() > 0) {
//                n = memberService.updateNonEmptyMemberDTOById(MemberDTO);
//            } else {
//                MemberDTO.setIsDel("0");
//                MemberDTO.setCreateTime(new Date());
//                n = memberService.insertMemberDTO(MemberDTO);
//            }
//        } else {
//            n = 2;
//        }
//        } catch(Exception e) {
//            e.printStackTrace();
//            n = 3;
//        }
//        AjaxUtil.ajaxJsonSucMessage(response, n);
//    }



}
